<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
	<TITLE></TITLE>
	<META NAME="GENERATOR" CONTENT="OpenOffice.org 3.2  (Unix)">
	<META NAME="CREATED" CONTENT="0;0">
	<META NAME="CHANGED" CONTENT="20110429;17050000">
	<META NAME="" CONTENT="">
</HEAD>
<BODY LANG="en-US" DIR="LTR">
<P><BR><BR>
</P>
<P ALIGN=CENTER><FONT SIZE=5><B>An experimental approach for speeding
Scala Scripts</B></FONT></P>
<P ALIGN=CENTER><BR><BR>
</P>
<P ALIGN=JUSTIFY STYLE="font-weight: normal"><FONT SIZE=4>This short
document describes an experimental approach for executing numerical
scripts in ScalaLab in significantly faster, i. e. about 20 to 150
times faster.</FONT></P>
<P ALIGN=JUSTIFY STYLE="font-weight: normal"><FONT SIZE=4>The Scala
Interpreter implements all defined names (vars, vals, functions,
modules) as members of synthetically generated classes and thereafter
implements an elaborate mechanism to realize the continuation of the
computation state.</FONT></P>
<P ALIGN=JUSTIFY STYLE="font-weight: normal"><FONT SIZE=4>This
mechanism is based on executing these classes and importing these
fields.</FONT></P>
<P ALIGN=JUSTIFY STYLE="font-weight: normal"><FONT SIZE=4>However,
for some reason, the JIT compiler is ineffective in field access and
thus numerical scripts executed directly at the Scala interpreter are
much slower than code placed inside functions/classes that utilizes
local variables instead.</FONT></P>
<P ALIGN=JUSTIFY STYLE="font-weight: normal"><FONT SIZE=4>For this
rationale, we introduce in ScalaLab a conveniency construct, the
&quot;<B>export var</B>&quot; statement. When this statement exist in
a script, ScalaLab automatically places the whole script code in the
&quot;userCompute$$&quot; function, and keeps global definitions as
fields only for the &quot;exported&quot; vars.</FONT></P>
<P ALIGN=JUSTIFY STYLE="font-weight: normal"><FONT SIZE=4>The
consequence is a dramatic speedup of numerical scripts.</FONT></P>
<P ALIGN=JUSTIFY><BR><BR>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=4><B>Example</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>// without exporting mechanism</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>tic;</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>var sm=0.0 </B></FONT>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>var m=0; var k=0; var l=0;</B></FONT></P>
<P ALIGN=JUSTIFY><BR><BR>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>while (m&lt;=500) {</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>sm=0.0; </B></FONT>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>k=0;</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>while (k&lt;=100) {</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>l=0;</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>while (l&lt;= 100) {</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>sm += (m*k*l)</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>l+=1</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>}</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>k +=1</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>}</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>m+=1</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>}</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>var tmWhile = toc()</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>println(&quot;tmWhile = &quot;+tmWhile+&quot;,
sm = &quot;+sm)</B></FONT></P>
<P ALIGN=JUSTIFY><BR><BR>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>// time = 1.061 secs</B></FONT></P>
<P ALIGN=JUSTIFY><BR><BR>
</P>
<P ALIGN=JUSTIFY><BR><BR>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>// with exporting mechanism</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>tic;</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>var sm=0.0 </B></FONT>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>var m=0; var k=0; var l=0;</B></FONT></P>
<P ALIGN=JUSTIFY><BR><BR>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>while (m&lt;=500) {</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>sm=0.0; </B></FONT>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>k=0;</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>while (k&lt;=100) {</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>l=0;</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>while (l&lt;= 100) {</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>sm += (m*k*l)</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>l+=1</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>}</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>k +=1</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>}</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>m+=1</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>}</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>export var smAll = sm</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>export var tmWhile = toc()</B></FONT></P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>println(&quot;tmWhile = &quot;+tmWhile+&quot;,
sm = &quot;+sm)</B></FONT></P>
<P ALIGN=JUSTIFY><BR><BR>
</P>
<P ALIGN=JUSTIFY><FONT SIZE=3><B>// time = 0.0090 sec</B></FONT></P>
<P ALIGN=JUSTIFY><BR><BR>
</P>
<P ALIGN=JUSTIFY><BR><BR>
</P>
</BODY>
</HTML>